
package snail.misc;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import snail.data.*;
import snail.model.User;

/**
 * Den här klassen används istället för ModelAndView för att hela tiden
 * fylla modellen med data som template.tagx använder.
 *
 * @author hellmer
 */
public class PreparedModelAndView extends ModelAndView {

    private User user;

    protected void addAlwaysAvailableData() {
        this.addObject("categories", new CategoryDAO().getAll());
        if (user != null) {
            user = new UserDAO().getById(user.getUserId());
            if (user != null) {
                this.addObject("watched", user.getWatchedArticles());
            }
        }
    }

    public PreparedModelAndView(View view, String modelName, Object modelObject, HttpServletRequest request) {
        super(view, modelName, modelObject);
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }

    public PreparedModelAndView(String viewName, String modelName, Object modelObject, HttpServletRequest request) {
        super(viewName, modelName, modelObject);
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }

    public PreparedModelAndView(View view, Map model, HttpServletRequest request) {
        super(view, model);
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }

    public PreparedModelAndView(String viewName, Map model, HttpServletRequest request) {
        super(viewName, model);
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }

    public PreparedModelAndView(View view, HttpServletRequest request) {
        super(view);
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }

    public PreparedModelAndView(String viewName, HttpServletRequest request) {
        super(viewName);
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }

    public PreparedModelAndView(HttpServletRequest request) {
        super();
        user = (User) request.getSession().getAttribute("user");
        addAlwaysAvailableData();
    }
}
